#include "CanModule.h"
#include <iostream>
#include <queue>      // std::priority_queue
#include "crc_calc.h"
#pragma pack(push, 1) // 设置 1 字节对齐
typedef union
{
    uint32_t data;
    struct
    {
        uint32_t frame_idx : 7; // can帧序号
        uint32_t src_addr : 7;  // 源地址
        uint32_t dst_addr : 7;  // 目标地址
        uint32_t cmd : 7;       // 命令字
        uint32_t buf_flag : 1;  // 非末帧
        uint32_t resv : 3;
    } compts;
} can_id_t;
struct can_message
{
    can_id_t id;
    uint32_t len;
    uint8_t data[1024];
};
struct multe_can_state
{
    uint8_t start_flag;
    uint8_t exp_frame_idx;
};
#pragma pack(pop) // 设置 1 字节对齐
class MutuleCanModule
{
public:
    MutuleCanModule(const std::string &interface);
    void read_message(void *arg);
    ~MutuleCanModule(){};
    uint8_t multiframe_load_last_data(uint8_t frame_idx, can_frame frame);
    void multiframe_start( can_frame frame);
    uint8_t multiframe_load_data(uint8_t frame_idx, can_frame frame);
    void open_read(){can_module->open_read();};
    // 写CAN数据
    void mutule_write_data(uint32_t canId, uint8_t *data, uint16_t data_len);

private:
    struct ifreq ifr;             // 网络接口信息
    struct sockaddr_can can_addr; // CAN地址
    std::queue<can_frame> frames;
    struct can_message m_can_message;
    multe_can_state m_mutule_can_state;
    CanModule::ptr can_module;
};
